package cn.tedu.oa.sys.service.impl;

import cn.tedu.oa.common.annotation.LogAnnotation;
import cn.tedu.oa.common.exception.ServiceException;
import cn.tedu.oa.common.service.impl.BaseServiceImpl;
import cn.tedu.oa.common.util.ShiroUtils;
import cn.tedu.oa.sys.dao.DepartmentDao;
import cn.tedu.oa.sys.entity.Department;
import cn.tedu.oa.sys.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements DepartmentService{

	@Autowired
	private DepartmentDao departmentDao;

    /**
     * 添加部门
     * @param department    部门对象
     */
	@Override
	public void add(Department department) {
		try {
            department.setAddUser(ShiroUtils.getPrincipal().getName()); //获得当前登录用户名
        } catch (Exception e){
            throw new ServiceException("获取登录用户异常");
        }
		department.setAddTime(new Date());                          //设置添加时间
		department.setIsdel(0);                                     //设置用户状态码为0
        department.setRemark("新加");
		departmentDao.add(department);
	}

	@Override
	public void update(Department department) {
        try {
            department.setAddUser(ShiroUtils.getPrincipal().getName()); //获得当前登录用户名
        } catch (Exception e){
            throw new ServiceException("获取登录用户异常");
        }
		department.setUpdateTime(new Date());
        department.setRemark("修改");
		departmentDao.update(department);
	}

	@Override
	public List<Department> getDepartmentByName(String deptName) {
		return departmentDao.findDepartmentByName(deptName);
	}

	/*查找除了id之外的所有部门*/
    @Override
    public List<Department> getExceptId(Integer id) {
        return departmentDao.getExceptId(id);
    }
    /*查找ids的所有下属部门*/
    @Override
    public List<Department> findDivisions(Integer... id) {
        return departmentDao.findDivisions(id);
    }

    @Override
	public List<Department> getDepartmentPagerList(String criteria, Integer pageNo, Integer pageSize) {
        if (criteria != null) {
            criteria = criteria.trim();
        }
		if (pageNo == null || pageNo < 1 || pageSize == null || pageSize < 1) {
			throw new IllegalArgumentException("参数不合法，请联系运维人员");
		}
		/*假设pageSize=10*/
				/*1 (1-1) x 10 + 1 = 1*/
		/*2 (2-1) x 10 + 1 = 11*/
		/*3 (3-1) x 10 + 1 = 21*/ /*@ming*/
		int startIndex = (pageNo - 1) * pageSize + 1;
		/*1 1 + 10 - 1 = 10*/
		/*2 11 + 10 - 1 = 20*/
		/*3 21 + 10 - 1 = 30*/ /*@ming*/
		int endIndex = startIndex + pageSize - 1;
		List<Department> list = departmentDao.getDepartmentPagerList(criteria, startIndex, endIndex);
		if (list == null) {
			throw new ServiceException("没有查询到数据");
		}
		return list;
	}

	@Override
	public int deletes(String deleteUser,Integer... id) {
		//参数有效性验证
		if(id==null||id.length==0)
			throw new IllegalArgumentException("请先选中记录");
		//执行删除操作
		int rows=0;
		try{
			rows=departmentDao.deletes(deleteUser,id);
		}catch(Throwable e){
			e.printStackTrace();
			throw new ServiceException("系统维护中,稍等片刻");
		}
		//验证并返回结果
		if(rows==0)
			throw new ServiceException("记录可能已经不存在");
		return rows;
	}

	@LogAnnotation("部门：根据id查询部门")
	@Override
	public Department getDeptById(Integer id) {
		if (id == null || id < 0) {
			return null;
		}
		return departmentDao.get(id);
	}
}
